﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CreateStaticPages.aspx.cs" Inherits="Web.Admin.Page_System.CreateStaticPages" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>

<!DOCTYPE html>

<%="<!--[if IE 8]> <html class=\"ie8 no-js\"> <![endif]-->"%>
<%="<!--[if IE 9]> <html class=\"ie9 no-js\"> <![endif]-->"%>
<%="<!--[if !IE]><!-->"%>
<html>
<%="<!--<![endif]-->"%>
    <!-- BEGIN HEAD -->

    <head>
        <meta charset="utf-8" />
        <title><%=Evan.Common.CommFun.GetAppSetting("AdminTitle") %> - 后台管理系统</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta content="width=device-width, initial-scale=1" name="viewport" />
        <!-- #include file="../include/com_res_css.inc" -->
    </head>
    <!-- END HEAD -->

    <body class="page-header-fixed page-sidebar-closed-hide-logo page-content-white">
        <!-- BEGIN HEADER -->
        
        <!-- #include file="../include/top_bar.inc" -->

        <!-- END HEADER -->
        <!-- BEGIN HEADER & CONTENT DIVIDER -->
        <div class="clearfix"> </div>
        <!-- END HEADER & CONTENT DIVIDER -->
        <!-- BEGIN CONTAINER -->
        <div class="page-container">
            <!-- BEGIN SIDEBAR -->
            
            <!-- #include file="../include/left_menu.inc" -->
            
            <!-- END SIDEBAR -->
            <!-- BEGIN CONTENT -->
            <div class="page-content-wrapper">
                <!-- BEGIN CONTENT BODY -->
                <div class="page-content">
                    <!-- BEGIN PAGE HEADER-->
                    <!-- BEGIN THEME PANEL -->
                    <!-- #include file="../include/theme_panel.inc" -->
                    <h3 class="page-title">
                         页面生成 &nbsp;&nbsp;&nbsp;
                        <a class="btn btn-primary btn-sm createAll" href="javascript:;"><i class="fa fa-send"></i>&nbsp;&nbsp;&nbsp;开始生成</a>
                        <%--<a class="btn btn-error btn-sm create-stop" href="javascript:;"><i class="fa fa-stop"></i>&nbsp;&nbsp;&nbsp;停止生成</a>--%>
                    </h3>
                    <!-- END PAGE TITLE-->
                    <!-- END PAGE HEADER-->
                    <!-- BEGIN PAGE CONTENT-->
                    <div class="note note-info">
                        <h4>共 <%=Urls.Count %> 页面</h4>
                        <h5> 生成进度&nbsp;<span class="now-create-per">0</span>%&nbsp;（ <span class="now-create-count">0</span> / <span class="now-all-count"><%=Urls.Count %></span> ）&nbsp;&nbsp; <%--预计还需要 : x 分钟--%></h5>
                        <h5> 正在生成 : <span class="now-create-url">----</span></h5>
                    </div>
                    
                    <div class="portlet-body flip-scroll">
                            <table class="table table-bordered table-striped table-condensed">
                                <thead class="flip-content">
                                    <tr>
                                        <th class="text-center" style="width:50px;"> 状态 </th>
                                        <th> URL地址 </th>
                                        <th class="text-center" style="width:50px;"> 操作 </th>                                     
                                    </tr>
                                </thead>
                                <tbody class="url-body">
                                    <%--<tr class="font-red">
                                        <td class="text-center"> <span class="fa fa-times-circle "></span> </td>
                                        <td> /news/1.html </td>
                                        <td class="text-center"> <span class="fa fa-repeat"></span> </td>
                                    </tr>
                                    <tr class="font-green">
                                        <td class="text-center"> <span class="fa fa-check-circle"></span> </td>
                                        <td> /news/1.html </td>
                                        <td class="text-center"> <span class="fa fa-repeat"></span> </td>
                                    </tr>--%>
                                    <tr data-url="/index.html">
                                        <td class="text-center"> <span data-state="state" class="fa fa-question-circle"></span> </td>
                                        <td class="this-url" data-url="/index.html"> /index.html </td>
                                        <td class="text-center"> <span class="fa fa-repeat tooltips create" style="cursor:pointer" data-original-title="生成此页面" data-url="/index.html"></span> </td>
                                    </tr>
                                    <% foreach (var item in Urls) { %>
                                    <tr data-url="<%=item %>">
                                        <td class="text-center"> <span data-state="state" class="fa fa-question-circle"></span> </td>
                                        <td class="this-url" data-url="<%=item %>"> <%=item %> </td>
                                        <td class="text-center"> <span class="fa fa-repeat tooltips create" style="cursor:pointer" data-original-title="生成此页面" data-url="<%=item %>"></span> </td>
                                    </tr>       
                                    <% } %>
                                    
                                </tbody>
                            </table>
                        </div>

                    <!-- END PAGE CONTENT-->
                </div>
                <!-- END CONTENT BODY -->
            </div>
            <!-- END CONTENT -->
            
        </div>
        <!-- END CONTAINER -->
        <!-- #include file="../include/footer.inc" -->
        <!-- #include file="../include/com_res_js.inc" -->
        <script type="text/javascript">


            var CreateInfo = {};
            CreateInfo.Count = <%=Urls.Count %>;
            CreateInfo.Created = 0;
            CreateInfo.Success = 0;
            CreateInfo.Error = 0;
            CreateInfo.CreateOnePage = false;
            CreateInfo.IsStop = false;
            CreateInfo.IsCreate = false;
            CreateInfo.Sleep = 200; //请求间隔 / ms
            CreateInfo.Creating = false;

            //生成递归的各种计算
            function CreateServ(i){

                //如果只生成一个页面  那直接返回
                if (CreateInfo.CreateOnePage) {
                    CreateInfo.CreateOnePage = false;
                    CreateInfo.Creating = false;
                    return false;
                }
                if (CreateInfo.IsStop) {
                    $(".now-create-url").text("----");
                    return false;
                    //alert("全部生成成功 , 详细情况请见列表");
                }

                //如果
                var now_page_no = i+1;
                if (now_page_no<CreateInfo.Count) {
                    //需要继续进行下一个
                    if (CreateInfo.Sleep>0) {
                        setTimeout(function(){ 
                            CreateStaticPages(i+1); 
                        },CreateInfo.Sleep);
                    } 
                    else{
                        CreateStaticPages(i+1);
                    }
                }
                else{
                    //列表全部遍历完成了
                    CreateInfo.IsStop = false;  //停止生成
                    CreateInfo.Creating = false; //生成状态改变
                    $(".now-create-url").text("----");
                    alert("全部生成成功 , 详细情况请见列表");
                }
            }

            //设置生成状态 传入地址和生成状态
            function CreateStateShow(i,state) {
                var url = $(".create").eq(i).attr("data-url");
                var _this = $(".url-body tr[data-url='" + url + "']");
                if (state) {
                    _this.find("span[data-state='state']").attr("class", "fa fa-check-circle");
                    _this.attr("class", "font-green");
                    CreateInfo.Created = CreateInfo.Created+1;
                }
                else {
                    _this.find("span[data-state='state']").attr("class", "fa fa-times-circle");
                    _this.attr("class", "font-red");
                    CreateInfo.Error = CreateInfo.Error+1;
                }
                CreateInfo.Created = CreateInfo.Created +1;
                CreateServ(i);
            }

            //设置生成状态
            function SetCreateStates(i,url)
            {
                if (CreateInfo.CreateOnePage) {
                    return false;
                }
                $(".now-create-count").text(i+1);
                $(".now-create-url").text(url);
                
                if (CreateInfo.Count>0) {
                    var per = parseFloat(i+1)/parseFloat($(".now-all-count").text());
                    $(".now-create-per").text(parseFloat(per*100).toFixed(2));
                }
            }

            //生成静态页入口    传入地址
            function CreateStaticPages(i) {
                var url = $(".create").eq(i).attr("data-url");
                SetCreateStates(i,url); //设置生成状态
                var data = { site: "<%=SiteConfig.WebUrl%>", page: url, folder: "<%=SiteConfig.WebStaticPath%>" };
                AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "CreateStaticPage", data, function (f, d) {
                    //console.log(f, d);
                    if (f) {
                        //请求成功
                        if (parseInt(d.result) == 1) {
                            CreateStateShow(i,true);
                        }
                        else if (parseInt(d.result) == -99) {
                            CreateInfo.IsStop = true;  //停止生成
                            CreateInfo.Creating = false; //生成状态改变
                            alert("登录超时 , 请重新登录", function () {
                                setTimeout(function () {
                                    JumpLogin();
                                }, 500);
                            });
                        }
                        else if (parseInt(d.result) == -1) {
                            CreateStateShow(i, false);
                        }
                        else {
                            CreateStateShow(i, false);
                        }
                    } else {
                        //请求失败
                        CreateStateShow(i, false);
                    }
                });
            }

            

            //生成静态页
            $(function () {
                $(".create").on("click", function () {
                    var _this = $(this);
                    CreateInfo.CreateOnePage = true;
                    CreateInfo.Creating = true;
                    CreateInfo.IsStop = false;
                    CreateStaticPages($(".create").index(_this));
                });

                $(".createAll").on("click", function () {
                    if (CreateInfo.IsCreate==false) {
                        CreateInfo.Creating = true;
                        CreateInfo.IsStop = false;
                        if (CreateInfo.Count>0) {
                            CreateStaticPages(0);
                        }
                    }
                    else{
                        alert("正在生成 , 请耐心等待");
                    }
                });

                $(".create-stop").on("click",function(){
                    CreateInfo.Creating = false;
                    CreateInfo.IsStop = true;
                    $(".now-create-url").text("----");
                });

            });


            window.onbeforeunload=function(e){     
                if (CreateInfo.Creating) {
                    var e = window.event||e;  
                    e.returnValue=("当前正在生成页面 , 离开页面后生成终止 , 是否确认离开 ？");
                }
            }

        </script>
    </body>
  
</html>

